package com.sf.wc.wordcount;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;

import com.sf.wc.constant.Constant;
import com.sf.wc.filereader.TxtFileReader;
import com.sf.wc.wordfilter.WordFilter;

/**
 * 描述：文字统计解决器
 * 
 * <pre>HISTORY
 * ****************************************************************************
 *  ID   DATE           PERSON          REASON
 *  1    2017年12月5日      Simba.Hua         Create
 * ****************************************************************************
 * </pre>
 * @author Simba.Hua
 */
public class WordCountResolver {
	private TxtFileReader fileReader;
	private WordFilter wordFilter;
	
	public WordCountResolver(TxtFileReader fileReader, WordFilter wordFilter) {
		super();
		this.fileReader = fileReader;
		this.wordFilter = wordFilter;
	}
	/**
	 * 
	 * @return
	 */
	public List<Entry<Character, Integer>> resolver() {
		Stream<String> allword = fileReader.read();
		if(allword != null) {
			allword.forEach(word -> wordFilter.doFilter(word));
		}
		return preTenCharactor();
	}
	private List<Entry<Character, Integer>> preTenCharactor() {
		List<Map.Entry<Character,Integer>> allwordList = new ArrayList<>(wordFilter.getCounter().entrySet());
		allwordList.sort((o1,o2) -> o2.getValue() - o1.getValue());
		if (allwordList.size() >= Constant.TOP_NUM ) {
			return allwordList.subList(0, Constant.TOP_NUM);	
		}
		return allwordList;
	}
}
